<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'secure/access_securities.php';
class Control_settings extends Access_securities {

	public function __construct(){
		parent::__construct("control-jobseekers");
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		$data['title']="Manage Jobseekers";
		$data['jobseekers']=$this->model_globals->select_all('job_seekers')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function sign_up()
	{
		$data['title']="Manage Jobseekers";
		$data['jobseekers']=$this->model_globals->select_all('job_seekers')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function banners()
	{
		$data['title']="Manage Jobseekers";
		$data['jobseekers']=$this->model_globals->select_all('job_seekers')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function banner_areas()
	{
		$data['title']="Manage Jobseekers";
		$data['jobseekers']=$this->model_globals->select_all('job_seekers')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function jobseeker_files()
	{
		$data['title']="Manage Jobseekers";
		$data['jobseekers']=$this->model_globals->select_all('job_seekers')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function categories()
	{
		$data['title']="Manage Jobseekers";
		$data['categories']=$this->model_globals->select_all('job_categories')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_category(){
		$data=array(
			'name'=>$this->input->post('category'),
			'status'=>1,
			'created_date'=>null
		);
		$is_inserted=$this->model_globals->insert('job_categories',$data);
		if($is_inserted){
			$this -> session -> set_flashdata('success', 'New category  was created success !.');
			redirect('control-settings/categories');
		}else{
			$this -> session -> set_flashdata('error', 'New category  was created fail !.');
			redirect('control-settings/categories');
		}
	}
	public function update_category(){
		
		$category_id=$this->uri->segment(3);
		$data=array(
			'name'=>$this->input->post('category')
		);
		$is_updated=$this->model_globals->update('job_categories',$data,array('job_category_id'=>$category_id));
		if($is_updated){
			$this -> session -> set_flashdata('success', 'Category information  was updated success !.');
			redirect('control-settings/categories');
		}else{
			$this -> session -> set_flashdata('error', 'Category information  was updated fail !.');
			redirect('control-settings/categories');
		}
	}
	public function delete_category(){
		
		$category_id=$this->uri->segment(3);
		
		$is_deleted=$this->model_globals->update('job_categories',array('job_category_id'=>$category_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Category information  was deleted success !.');
			redirect('control-settings/categories');
		}else{
			$this -> session -> set_flashdata('error', 'Category information  was deleted fail !.');
			redirect('control-settings/categories');
		}
	}
	public function view_sub_category()
	{
		$data['title']="Manage Jobseekers";
		$data['sub_categories']=$this->model_globals->select_where('job_sub_categories',array('using_job_category_id'=>$this->uri->segment(3)))->result();
		$data['category_name']=$this->model_globals->select_string('job_categories','name',array('job_category_id'=>$this->uri->segment(3)));
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_sub_category(){
		
		$job_category_id=$this->input->post('job_category_id');
		$data=array(
			'using_job_category_id'=>$job_category_id,
			'name'=>$this->input->post('sub_category'),
			'status'=>1,
			'created_date'=>null
		);
		$is_inserted=$this->model_globals->insert('job_sub_categories',$data);
		if($is_inserted){
			$this -> session -> set_flashdata('success', 'New sub category  was created success !.');
			redirect('control-settings/view-sub-category/'.view_sub_category);
		}else{
			$this -> session -> set_flashdata('error', 'New subcategory  was created fail !.');
			redirect('control-settings/view-sub-category/'.view_sub_category);
		}
	}
	public function update_sub_category(){
		
		$job_category_id=$this->input->post('job_category_id');
		$sub_category_id=$this->uri->segment(3);
		$data=array(
			'name'=>$this->input->post('sub_category')
		);
		$is_updated=$this->model_globals->update('job_sub_categories',$data,array('job_sub_category_id'=>$sub_category_id));
		if($is_updated){
			$this -> session -> set_flashdata('success', 'Sub category information  was updated success !.');
			redirect('control-settings/view-sub-category/'.$job_category_id);
		}else{
			$this -> session -> set_flashdata('error', 'Sub category information  was updated fail !.');
			redirect('control-settings/view-sub-category/'.$job_category_id);
		}
	}
	public function delete_sub_category(){
		
		//$job_category_id=$this->input->post('job_category_id');
		$sub_category_id=$this->uri->segment(3);
		
		$is_deleted=$this->model_globals->delete('job_sub_categories',array('job_sub_category_id'=>$sub_category_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Sub category information  was deleted success !.');
			redirect('control-settings/categories');
		}else{
			$this -> session -> set_flashdata('error', 'Sub category information  was deleted fail !.');
			redirect('control-settings/categories');
		}
	}
	public function locations()
	{
		$data['title']="Manage Jobseekers";
		$data['locations']=$this->model_globals->select_all('job_locations')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_location(){
		$data=array(
			'name'=>$this->input->post('location'),
			'status'=>1,
			'created_date'=>null
		);
		$is_inserted=$this->model_globals->insert('job_locations',$data);
		if($is_inserted){
			$this -> session -> set_flashdata('success', 'New location  was created success !.');
			redirect('control-settings/locations');
		}else{
			$this -> session -> set_flashdata('error', 'New location  was created fail !.');
			redirect('control-settings/locations');
		}
	}
	public function edit_location(){
		$location_id=$this->input->post('location_id');
		echo $location_id;
	}
	public function update_location(){
		$location_id=$this->uri->segment(3);
		$data=array(
			'name'=>$this->input->post('location')
		);
		$is_updated=$this->model_globals->update('job_locations',$data,array('job_location_id'=>$location_id));
		if($is_updated){
			$this -> session -> set_flashdata('success', 'Location information  was updated success !.');
			redirect('control-settings/locations');
		}else{
			$this -> session -> set_flashdata('error', 'Location information  was updated fail !.');
			redirect('control-settings/locations');
		}
	}
	public function delete_location(){
		$location_id=$this->uri->segment(3);
		
		$is_deleted=$this->model_globals->delete('job_locations',array('job_location_id'=>$location_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Location information  was deleted success !.');
			redirect('control-settings/locations');
		}else{
			$this -> session -> set_flashdata('error', 'Location information  was deleted fail !.');
			redirect('control-settings/locations');
		}
	}
	public function companies()
	{
		$data['title']="Manage Jobseekers";
		$data['companies']=$this->model_globals->select_all('companies')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_company() {
		
		$data['title'] = "Create company";	
		$this -> form_validation -> set_rules('company', 'company', 'required|min_length[2]|max_length[50]');

		if ($this -> form_validation -> run() === FALSE) {
			$this -> load -> view(B_MASTER . "index", $data);
		} else {
			$config['upload_path'] = './Templates/F-Template/F-IMAGES/employer_logo/';
			$config['allowed_types'] = 'gif|jpg|jpeg|png';
			$config['max_size'] = '30000';
			$this -> load -> library('upload', $config);

			if (!$this -> upload -> do_upload()) {
				$data['errors']= array('error' => $this->upload->display_errors());
				
				$this -> load -> view(B_MASTER . "index", $data);
			} else {
				
				$data = $this -> upload -> data();
				$image_name = $data['file_name'];

				$img_width = $data['image_width'];
				$img_height = $data['image_height'];

				$scale_height = 120 / $img_height;
				$fit_width = $img_width * $scale_height;
				$fit_height = $img_height * $scale_height;

				$config['image_library'] = 'gd2';
				$config['source_image'] = "./Templates/F-Template/F-IMAGES/employer_logo/$image_name";
				$config['create_thumb'] = TRUE;
				$config['maintain_ratio'] = TRUE;
				$config['width'] = $fit_width;
				$config['height'] = $fit_height;

				$this -> load -> library('image_lib', $config);

				$this -> image_lib -> resize();
				if (!$this -> image_lib -> resize()) {
					echo $this -> image_lib -> display_errors();
					exit ;
				}
				
				$data_company = array('using_employer_id' => 0, 'name' => $this -> input -> post('company', TRUE), 'description' => $this -> input -> post('company_desc', TRUE), 'address' => $this -> input -> post('address', TRUE), 'phone' => $this -> input -> post('phone', TRUE), 'fax' => $this -> input -> post('fax', TRUE), 'website' => $this -> input -> post('website', TRUE), 'logo' =>$image_name, 'status' => 1, 'created_date' => null);
				$is_inserted = $this -> model_globals -> insert('companies', $data_company);
				if ($is_inserted) {
					$this -> session -> set_flashdata('success', 'New company was created success !');
					redirect('control-settings/companies');
				}else{
					$this -> session -> set_flashdata('error', 'New company was created fail!');
					redirect('control-settings/companies');
				}
			}
				
		}

	}
	public function edit_company() {
		
		$data['title'] = "Edit company";	
		$this -> form_validation -> set_rules('company', 'company', 'required|min_length[2]|max_length[50]');
		$company_id=$this->uri->segment(3);
		if ($this -> form_validation -> run() === FALSE) {
			$data['company_info']=$this->model_globals->select_where('companies',array('company_id'=>$company_id));
			$this -> load -> view(B_MASTER . "index", $data);
		} else {
			$image_name="";
			$error_upload=FALSE;
				if (!empty($_FILES['userfile']['name'])){
					$config['upload_path'] = './Templates/F-Template/F-IMAGES/employer_logo/';
					$config['allowed_types'] = 'gif|jpg|jpeg|png';
					$config['max_size'] = '30000';
					$this -> load -> library('upload', $config);
		
					if (!$this -> upload -> do_upload()) {
						$data['errors']= array('error' => $this->upload->display_errors());
						$error_upload=TRUE;
						$data['company_info']=$this->model_globals->select_where('companies',array('company_id'=>$company_id));
						$this -> load -> view(B_MASTER . "index", $data);
					} else {
						
						$data = $this -> upload -> data();
						$image_name = $data['file_name'];
		
						$img_width = $data['image_width'];
						$img_height = $data['image_height'];
		
						$scale_height = 120 / $img_height;
						$fit_width = $img_width * $scale_height;
						$fit_height = $img_height * $scale_height;
		
						$config['image_library'] = 'gd2';
						$config['source_image'] = "./Templates/F-Template/F-IMAGES/employer_logo/$image_name";
						$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = TRUE;
						$config['width'] = $fit_width;
						$config['height'] = $fit_height;
		
						$this -> load -> library('image_lib', $config);
		
						$this -> image_lib -> resize();
						if (!$this -> image_lib -> resize()) {
							echo $this -> image_lib -> display_errors();
							exit ;
						}
					}
				}
				if($image_name!=""){
					$data_company = array('name' => $this -> input -> post('company', TRUE), 'description' => $this -> input -> post('company_desc', TRUE), 'address' => $this -> input -> post('address', TRUE), 'phone' => $this -> input -> post('phone', TRUE), 'fax' => $this -> input -> post('fax', TRUE), 'website' => $this -> input -> post('website', TRUE), 'logo' =>$image_name);
				}else{
					$data_company = array('name' => $this -> input -> post('company', TRUE), 'description' => $this -> input -> post('company_desc', TRUE), 'address' => $this -> input -> post('address', TRUE), 'phone' => $this -> input -> post('phone', TRUE), 'fax' => $this -> input -> post('fax', TRUE), 'website' => $this -> input -> post('website', TRUE));
				}
			
				if($error_upload!=TRUE){
					$is_updated = $this -> model_globals -> update('companies', $data_company,array('company_id'=>$company_id));
					if ($is_updated) {
						$this -> session -> set_flashdata('success', 'Selected company was modified success !');
						redirect('control-settings/companies');
					}else{
						$this -> session -> set_flashdata('error', 'Selected company was modified fail!');
						redirect('control-settings/companies');
					}	
				}
				
		}

	}
	public function delete_company(){
		$company_id=$this->uri->segment(3);
		
		$is_deleted=$this->model_globals->delete('companies',array('company_id'=>$company_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Company information  was deleted success !.');
			redirect('control-settings/companies');
		}else{
			$this -> session -> set_flashdata('error', 'Company information  was deleted fail !.');
			redirect('control-settings/companies');
		}
	}
}
